<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-14 21:25:20
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-22 22:30:02
 */

use app\modules\diandi_shop\models\DdOrder;
use common\addons\diandi_shop\models\enums\OrderStatus;
use richardfan\widget\JSRegister;
use yii2mod\editable\bundles\EditableDatePickerAsset;
use yii2mod\editable\EditableColumn;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $searchModel app\modules\diandi_shop\models\searchs\DdOrderSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Dd Orders';
$this->params['breadcrumbs'][] = $this->title;
$status =  Yii::$app->request->queryParams;
$order_status = $status['DdOrderSearch']['order_status'];
?>
<ul class="nav nav-tabs">
    <li class="<?= isset($order_status)  ? '' : 'active' ?>">
        <?= Html::a('全部', ['index'], ['class' => '']) ?>
    </li>
    <?php foreach (OrderStatus::listData() as $key => $item) : ?>
        <li class="<?= $order_status == $key && $order_status != '' ? 'active' : '' ?>">
            <?= Html::a($item, Url::to(['index', 'DdOrderSearch[order_status]' => $key]), ['class' => '']) ?>
        </li>
    <?php endforeach; ?>



</ul>
<div class="firetech-main" style="margin-top:20px;">

    <div class="dd-order-index ">
        <?php // echo $this->render('_search', ['model' => $searchModel]); 
        ?>
        <div class="panel panel-default">
            <div class="box-body table-responsive">
                <?=
                    GridView::widget([

                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'id' => 'ordergrid',
                        'showFooter' => true,  //设置显示最下面的footer
                        'columns' => [
                            // 'order_id',
                            [
                                'class' => 'yii\grid\CheckboxColumn',
                                'name' => 'id',
                                'headerOptions' => ['width' => '30'],
                                'footer' => '<button href="#" class="btn  btn-primary"  id="deletes">批量删除</button>',
                                // 'footerOptions' => ['colspan' => 2],
                            ],
                            [
                                'label' => '订单编号',
                                'attribute' => 'order_no',
                                'value' => function ($date) {
                                    return $date->order_no;
                                },
                                'headerOptions' => ['style' => 'width:120px;'],
                                'footer' => '<button  class="btn  btn-primary"  id="ExportData">批量导出</button>',
                            ],
                            'order_no',
                            [
                                'label' => '订单状态',
                                'attribute' => 'order_status',
                                'filter' => Html::dropDownList('DdOrderSearch[order_status]', $searchModel->order_status, OrderStatus::listData(), [
                                    'prompt' => ['text' => '全部', 'options' => ['value' => '']],
                                    'class' => 'form-control'
                                ]),
                                'value' => function ($date) {
                                    return OrderStatus::getLabel($date->order_status);
                                },
                                'headerOptions' => ['style' => 'width:120px;'],
                            ],
                            [
                                'label' => '下单时间',
                                'attribute' => 'create_time',
                                'filter' => \yii\jui\DatePicker::widget([
                                    'name' => 'DdOrderSearch[create_time]',
                                    'options' => [
                                        'id' => 'create_time_id',
                                        'class' => 'form-control'
                                    ]
                                ]),
                                'value' => function ($date) {
                                    return date('Y-m-d H:i:s', $date->create_time);
                                },
                                // 'headerOptions' => ['style' => 'width:120px']
                            ],
                            // 'create_time:datetime',  
                            'total_price',
                            'pay_price',
                            'pay_status',
                            //'pay_time:datetime',
                            // 'express_price',
                            //'express_company',
                            //'express_no',
                            //'delivery_status',
                            //'delivery_time:datetime',
                            //'receipt_status',
                            //'receipt_time:datetime',
                            //'transaction_id',
                            //'user_id',
                            //'wxapp_id',
                            //'update_time:datetime',
                            // [
                            //     'class' => EditableColumn::class,
                            //     'attribute' => 'order_status',
                            //     'url' => ['change-username'],
                            //     'type' => 'select',
                            //     'editableOptions' => function ($model) {
                            //         return [
                            //             'source' => OrderStatus::listData(),
                            //             'value' => $model->order_status,
                            //         ];
                            //     },
                            // ],
                            // [
                            //     'class' => EditableColumn::class,
                            //     'attribute' => 'total_price',
                            //     'url' => ['change-username'],
                            // ],
                            [
                                'class' => 'yii\grid\ActionColumn',
                                'header' => '操作',
                                'template' => '{view}',
                                'buttons' => [
                                    'view' => function ($url, $model, $key) {
                                        return  Html::a('<button type="button" class="btn btn-block btn-primary btn-sm">订单详情</button>', $url, ['title' => '订单详情']);
                                    },
                                ],
                                // 'buttons' => [],
                                'headerOptions' => ['width' => '100']
                            ],
                        ],
                    ]); ?>
            </div>
        </div>
    </div>
</div>

<?php JSRegister::begin([
    'id' => 'orders'
]);
?>
<script>
    $('#deletes').click(function(event) {
        console.log(event)
        var ids_v = $("#ordergrid").yiiGridView("getSelectedRows");
        console.log(ids_v)
        $.ajax({
            type: 'POST',
            url: "<?= Url::toRoute(['order/dd-order/deletes']) ?>",
            data: {
                'ids': ids_v
            },
            dataType: 'JSON',
            success: function(res) {
                if (res.code == 200) {
                    alert('删除成功')
                    window.location.reload();
                }
                console.log(res)
            },
        });
    })

    $('#ExportData').click(function(event) {
        console.log(event)
        var ids_v = $("#ordergrid").yiiGridView("getSelectedRows");
        window.open("<?= Url::toRoute(['order/dd-order/exportdata']) ?>")
    })
</script>
<?php JSRegister::end(); ?>